<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>js typeof&instanceof</title>
</head>
<body>
  <p>typeof&instanceof</p>
  <ul>
    <li>使用typeof判断基础类型，使用instanceof判断引用类型</li>
    <li>typeof无法区分数组和对象，必须通过instanceof来区分</li>
    <li>对未声明或未赋值的变量，typeof的检测结果为undefined</li>
  </ul>
  <script>
    const num = 10
    const name = 'Miracle'
    const isMarried = true
    const getName = () => name
    let userName
    const skills = ['js', 'es', 'ts']
    const user = { name: 'Miracle', age: 35 }
    function Student() {}
    const stu = new Student()
    // 使用typeof检测
    console.log(typeof num)        // number
    console.log(typeof name)       // string
    console.log(typeof isMarried)  // boolean
    console.log(typeof getName)    // function
    console.log(typeof userName)   // undefined
    console.log(typeof userAge)    // undefined
    // 对于数组和对象，使用typeof无法区分
    console.log(typeof skills)     // object
    console.log(typeof user)       // object
    // 使用instanceof检测
    console.log(skills instanceof Array) // true
    console.log(user instanceof Object)  // true
    console.log(stu instanceof Student)  // true
  </script>
</body>
</html>